class Solution {
public:
    int maxSumDivThree(vector<int>& nums) {
        int x1=INT_MAX/4;
        int x2=x1;
        int y1=INT_MAX/4;
        int y2=y1;
        int n=nums.size();
        int sum=0;
        for(int i=0;i<n;++i)
        {
            sum+=nums[i];
            if(nums[i]%3==1)
            {
                if(nums[i]<x1)
                {
                    x2=x1;
                    x1=nums[i];
                }
                else if(nums[i]<x2)
                {
                    x2=nums[i];
                }
            }
            if(nums[i]%3==2)
            {
                if(nums[i]<y1)
                {
                    y2=y1;
                    y1=nums[i];
                }
                else if(nums[i]<y2)
                {
                    y2=nums[i];
                }
            }
        }
        if(sum%3==0) return sum;
        if(sum%3==1)
        {
            return max(sum-x1,sum-y1-y2);
        }
    
        if(sum%3==2)
        {
            return max(sum-x1-x2,sum-y1);
        }
        return 0;
    }
};
